package com.fantsey.event;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 邮件业务类
 *
 * @date 2024/3/8 14:35
 * @author fanxin
 * @since 1.0
 */
@Component
public class EmailEventPublisher implements ApplicationEventPublisherAware {

    @Resource
    private ApplicationEventPublisher publisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }

    /**
     * 发送邮件
     *
     * @param address 邮件地址
     * @param content 邮件内容
     */
    public void sendEmail(String address, String content) {

        // 发送邮件的逻辑
        System.out.println("发送邮件：" + address + "，" + content);
        Long startTime = System.currentTimeMillis();

        // 发布邮件发送事件
        publisher.publishEvent(new EmailSendEvent(this, address, content));

        Long endTime = System.currentTimeMillis();
        long takeTime = (endTime - startTime)/1000;

        System.out.println("邮件发送完毕！消耗时长：" + takeTime + "秒");
    }

}
